package cn.itcast.bos.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.IFixedAreaDao;
import cn.itcast.bos.dao.base.ITakeTimeDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.IFixedAreaService;

@Controller
@Transactional
public class FixedAreaServiceImpl implements IFixedAreaService{
	@Autowired
	private IFixedAreaDao fixedAreaDao;
	@Autowired
	private ITakeTimeDao takeTimeDao;
	@Autowired
	private CourierRepository courierRepository;
	
	@Override
	public void save(FixedArea model) {
		fixedAreaDao.save(model);
	}

	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pb) {
		return fixedAreaDao.findAll(specification, pb);
	}

	@Override
	public void associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
		FixedArea fa = fixedAreaDao.findOne(model.getId());
		Courier courier = courierRepository.findOne(courierId);
		TakeTime takeTime = takeTimeDao.findOne(courierId);
		
		//将快递员关联到定区上
		fa.getCouriers().add(courier);
		//将收派关系关联到快递员上
		courier.setTakeTime(takeTime);
	}
	
	
}
